package uk.ac.bath.domains.misc;
import uk.ac.bath.domains.misc.FoodSupply;
import uk.ac.bath.legacy.Point;
import uk.ac.bath.util.*;
import java.io.*;
import java.util.*;
import java.awt.*;

public class PulseFood2D implements FoodSupply {


    public Vector<Tweakable> tweaks = new Vector<Tweakable>();

    public TweakableInt foodLimit       =  
	new TweakableInt(tweaks,0,1000,50,"MaxFood");

    public TweakableInt foodInc       =  
	new TweakableInt(tweaks,0,1000,50,"FoodGrowthRate");

    public Collection<Tweakable> getTweaks() { return tweaks;}

    double     food_[][];

    public int left;
    public int sizeX;
    public int top;
    public int sizeY;

    public PulseFood2D(int left,int size) {
	this(left,left+size,left,left+size);

//         sizeX = size;
// 	this.left = left;
// 	this.top = left;
// 	sizeY = size;
	//	initFood();
    }

//     private void initFood() {
//   
// 	int fLimit = foodLimit.getNumber().intValue();
// 	for (int i=0;i<sizeX;i++) {
// 	    for (int j=0;j<sizeY;j++) food_[i][j] = fLimit;
// 	}
//     }

    public PulseFood2D(int left,int right,int top,int bot) {
	//	assert left <= right;
	//  assert top  <= bot;
	this.left = left;
	this.top = top;

        sizeX = right - left + 1;
	sizeY = bot - top +1;
	food_ = new double[sizeX][sizeY];
    }


    public double foodLimit() {
	return foodLimit.getNumber().doubleValue();
    }

//     public Object clone() {
// 	return new PulseFood2D(left_,size_);
//     }

     public void replenishFood() {
 	double fLimit = foodLimit.getNumber().doubleValue();
	double fInc = foodInc.getNumber().doubleValue();

         for (int i=0 ; i < sizeX ; i++) {
 	    for (int j=0 ; j < sizeY ; j++) {
 		if (food_[i][j] < fLimit) food_[i][j] += fInc;
 	    }
         }
     }

    public double eatFoodAt(Point p1,double amount) {
	Point2DInt p = (Point2DInt)p1;
        int x = p.getIX();
        if (x >= sizeX + left ) return 0;
        if (x < left ) return 0;

        int y = p.getIY();
        if (y >= sizeY + top ) return 0;
        if (y < top ) return 0;

        double avail = food_[x-left][y-top];
	if (avail <= 1) return 0;
	//	assert amount >= 0;
        amount = java.lang.Math.min (amount,avail-1);
        food_[x-left][y-top] -= amount;
        return amount;
    }

    public double getFoodAt(Point p1){
	Point2DInt p = (Point2DInt)p1;
        int x = p.getIX();
        int y = p.getIY();
        return getFoodAt(x,y);
    }


    public double[][]   getArray() {
	return food_;

    }

    public Rectangle getBoundingRect() {
	return new Rectangle(left,top,sizeX,sizeY);
    }


    public double getFoodAt(int x,int y){

        if (x >= sizeX + left ) return 0;
        if (x < left ) return 0;

        if (y >= sizeY + top ) return 0;
        if (y < top ) return 0;
        return food_[x-left][y-top];
    }

    //    public void load(BufferedReader in) throws Exception {
// 	String data=in.readLine();
// 	size_ = Integer.parseInt(data);
//         food_ = new int[size_];
// 	for(int i=0; i< size_; i++) {
// 	    data=in.readLine();
// 	    food_[i] = Integer.parseInt(data);
// 	}
//    }


  //   public void save(PrintStream out) {
// // 	out.println(getClass().getName());
// // 	out.println(size_);

// // 	for(int i=0; i< size_; i++) {
// // 	    out.print(food_[i] + " ");
// // 	}
// // 	out.println();
//     }





    public Point randomPoint() {
	int x = left  + MyRandom.nextInt(sizeX);
	int y = top + MyRandom.nextInt(sizeY);
	return new Point2DInt(x,y);
    }



}
